﻿/*
 * @author: wizardc
 */

using System;
using HedgehogTeam.EasyTouch;
using UnityEngine;

namespace Dou.Touch
{
    /// <summary>
    /// EasyTouch 控制器
    /// </summary>
    public class EasyTouchController : MonoBehaviour
    {
        /// <summary>
        /// 按下时
        /// </summary>
        public event Action<float, float> onTouchDown;

        /// <summary>
        /// 弹起时
        /// </summary>
        public event Action<float, float> onTouchUp;

        /// <summary>
        /// 非 UI 区域单击时
        /// </summary>
        public event Action<float, float> onSimpleTap;

        /// <summary>
        /// 非 UI 区域双击时
        /// </summary>
        public event Action<float, float> onDoubleTap;

        /// <summary>
        /// 非 UI 区域开始滑动时
        /// </summary>
        public event Action<float, float> onSwipeStart;

        /// <summary>
        /// 非 UI 区域滑动时
        /// </summary>
        public event Action<float, float> onSwipe;

        /// <summary>
        /// 非 UI 区域结束滑动时
        /// </summary>
        public event Action<float, float> onSwipeEnd;

        /// <summary>
        /// 缩放时
        /// </summary>
        public event Action<float, float, float> onScale;
        
        /// <summary>
        /// 结束缩放时
        /// </summary>
        public event Action onScaleEnd;
        
        private bool _swiping = false;
        private bool _pinching = false;
        
        void OnEnable()
        {
            EasyTouch.SetGesturePriority(EasyTouch.GesturePriority.Tap);
            
            EasyTouch.On_TouchDown += OnTouchDown;
            EasyTouch.On_TouchUp += OnTouchUp;
            EasyTouch.On_SimpleTap += OnSimpleTap;
            EasyTouch.On_DoubleTap += OnDoubleTap;
            
            EasyTouch.On_DragStart += OnDragStart;
            EasyTouch.On_Drag += OnDrag;
            EasyTouch.On_DragEnd += OnDragEnd;
            
            EasyTouch.On_SwipeStart += OnSwipeStart;
            EasyTouch.On_Swipe += OnSwipe;
            EasyTouch.On_SwipeEnd += OnSwipeEnd;
            
            EasyTouch.On_Pinch += OnPinch;
            EasyTouch.On_TouchUp2Fingers += OnTouchUp2Fingers;
        }
        
        /// <summary>
        /// 开启或关闭 EasyTouch
        /// </summary>
        public void SetEnabled(bool active)
        {
            EasyTouch.SetEnabled(active);
        }
        
        private void OnTouchDown(Gesture gesture)
        {
            onTouchDown?.Invoke(gesture.position.x, gesture.position.y);
        }

        private void OnTouchUp(Gesture gesture)
        {
            onTouchUp?.Invoke(gesture.position.x, gesture.position.y);
        }

        private void OnSimpleTap(Gesture gesture)
        {
            onSimpleTap?.Invoke(gesture.position.x, gesture.position.y);
        }

        private void OnDoubleTap(Gesture gesture)
        {
            onDoubleTap?.Invoke(gesture.position.x, gesture.position.y);
        }

        private void OnDragStart(Gesture gesture)
        {
            OnSwipeStart(gesture);
        }

        private void OnDrag(Gesture gesture)
        {
            OnSwipe(gesture);
        }

        private void OnDragEnd(Gesture gesture)
        {
            OnSwipeEnd(gesture);
        }

        private void OnSwipeStart(Gesture gesture)
        {
            if (_pinching)
            {
                return;
            }
            _swiping = true;
            onSwipeStart?.Invoke(gesture.position.x, gesture.position.y);
        }

        private void OnSwipe(Gesture gesture)
        {
            if (_pinching || !_swiping)
            {
                return;
            }
            onSwipe?.Invoke(gesture.position.x, gesture.position.y);
        }

        private void OnSwipeEnd(Gesture gesture)
        {
            if (_pinching || !_swiping)
            {
                return;
            }
            _swiping = false;
            onSwipeEnd?.Invoke(gesture.position.x, gesture.position.y);
        }

        private void OnPinch(Gesture gesture)
        {
            if (_swiping)
            {
                return;
            }
            _pinching = true;
            onScale?.Invoke(gesture.position.x, gesture.position.y, gesture.deltaPinch);
        }

        private void OnTouchUp2Fingers(Gesture gesture)
        {
            if (_swiping || !_pinching)
            {
                return;
            }
            _pinching = false;
            onScaleEnd?.Invoke();
        }

        void OnDisable()
        {
            EasyTouch.On_TouchDown -= OnTouchDown;
            EasyTouch.On_TouchUp -= OnTouchUp;
            EasyTouch.On_SimpleTap -= OnSimpleTap;
            EasyTouch.On_DoubleTap -= OnDoubleTap;
            
            EasyTouch.On_DragStart -= OnDragStart;
            EasyTouch.On_Drag -= OnDrag;
            EasyTouch.On_DragEnd -= OnDragEnd;
            
            EasyTouch.On_SwipeStart -= OnSwipeStart;
            EasyTouch.On_Swipe -= OnSwipe;
            EasyTouch.On_SwipeEnd -= OnSwipeEnd;
            
            EasyTouch.On_Pinch -= OnPinch;
            EasyTouch.On_TouchUp2Fingers -= OnTouchUp2Fingers;
            
            _swiping = false;
            _pinching = true;
        }
    }
}
